
import { bind_scence_event } from "./event"
import {getlev, getHitPoint,playBoomSound} from "./script"
import { Sky, Logo, StartBtn, RankBtn, Hero, PauseBtn, Bhq, Score, Life, Level, ContinueBtn, SaveBtn, EndBtn, Bullet, GameAgainBtn, EndScoreBtn, EnemyList, BulletsList,EBsList ,BounzList,BackGroundDrawer,UfoList} from "./obj"
import {Enum} from "./enum.js"

var scenceList = {}
  
export function changeScence(name){
    if(!globalData.initScence){
        scenceList = {
            sc1: new startGame(),
            sc2: new playGame(),
            sc3: new pauseGame(),
            sc4: new endGame()
        }
        globalData.initScence = true
    }
    canvas.onclick=null
    canvas.ontouchstart=null;
    scenceList[name].show()
    bind_scence_event(scenceList[name])
}

export function excuteScenceFun(sc,fun){
    scenceList[sc][fun]()
}


export function startGame() {
    // 场景一元素
    this.btn = {
        BackGroundDrawer: new BackGroundDrawer(),
        logo: new Logo(),
        start_btn: new StartBtn(),
        rank_btn: new RankBtn()
    }
    this.show = function () {
        this.btn.BackGroundDrawer.paint(5);
        this.btn.logo.paint();
        this.btn.start_btn.paint();
        this.btn.rank_btn.paint();
    }
}

export function playGame() {
    var gamer = false
    var _this = this
    this.btn = {
        pause_btn: new PauseBtn(),
    }
    this.view = {
        sky: new Sky(),
        BulletsList: new BulletsList(),
        EnemyList: new EnemyList(),
        UfoList: new UfoList(),    
        BounzList: new BounzList(),
        view_score: new Score(),
        view_life: new Life(),
        view_level: new Level(),
    }
    this.view.EBsList = new EBsList(this.view.EnemyList.enemies),
    this.painter = ()=>{
        
        if (!gamer) {
            return
        }

        this.view.sky.update();
       
        //hero的子弹
        this.view.BulletsList.update()

        //敌机的子弹
        this.view.EBsList.update()

        //hero
        globalData.hero.paint()

        //保护罩
        this.view.bhq.paint() 

        //敌飞机产生，绘制
        this.view.EnemyList.update()

        //击中爆破效果
        this.view.BounzList.update()

        this.view.UfoList.update()
        this.view.view_score.paint();
        this.view.view_life.paint();
        this.view.view_level.paint();
        this.btn.pause_btn.paint();

        this.checkHit()
        // getlev(); 
        this.isGameOver();//有关界面渲染，放到最后
       
        window.requestAnimationFrame(this.painter.bind(this))
    }
    this.begin = ()=>{
        if (gamer) {
            gamer = false
            return
        }
        gamer = true
        window.requestAnimationFrame(this.painter.bind(this));
    }
    this.show = function () {
        globalData.hero = new Hero()
        this.view.bhq =  new Bhq(globalData.hero)
        this.begin()
    }
    this.continue = ()=>{
        this.begin()
        bind_scence_event(this)
    }
    this. isGameOver = ()=> {
        if (globalData.hero.life <= 0) {
           window.changeScence('sc4');
        }
    }
    this.checkHit =()=>{
        var enemies = this.view.EnemyList.enemies
        var bullets = this.view.BulletsList.bullets
        var bounzList = this.view.BounzList
        var eBullets = this.view.EBsList.eBullets
        var bhq = this.view.bhq
        var ufos = this.view.UfoList.ufos
        var hero = globalData.hero
        //检测敌机碰撞
        for (var i = 0; i < enemies.length; i++) {
            //敌机是否被打中
            var e = enemies[i];
            for (var j = 0; j < bullets.length; j++) {
                var b = bullets[j];
                if (e.x > b.x - e.img.width && e.x < b.x + b.img.width && e.y > b.y - e.img.height && e.y < b.y + b.img.height) {
                    e.life--;
                    bounzList.make(e.x, e.y, e.type);
                    if (e.life <= 0) {
                        e.canDelete = true;
                        globalData.score += e.score;
                        getlev()
                        playBoomSound()
                    }
                    b.canDelete = true;
                    
                }
            }

            //检测是否和英雄机碰撞
            if (e.x > hero.x - e.img.width && e.x < hero.x + hero.img.width && e.y > hero.y - e.img.height && e.y < hero.y + hero.img.height) {
                e.canDelete = true;
                bounzList.make(e.x, e.y, e.type);
                playBoomSound()

                if (!bhq.startBhq) {
                    hero.life--;
                    hero.type--;
                    if (hero.type <= 0) {
                        hero.type = 0;
                    }
                    Sounds.gamerOver.play();
                    var hitPoint = getHitPoint(e, hero)
                    hero.boom(hitPoint.x, hitPoint.y);
                }
               
            }

            //检测是否和保护罩发生碰撞
            if (bhq.startBhq) {
                if (e.x > bhq.x - e.img.width && e.x < bhq.x + bhq.imgs[0].width && e.y > bhq.y - e.img.height && e.y < bhq.y + bhq.imgs[0].height) {
                    e.canDelete = true;
                    bounzList.make(e.x, e.y, e.type);
                    playBoomSound()
                }
            }
    
        }

        //检测hero是否和ufo发生碰撞
        for (var i = 0; i < ufos.length; i++) {
            var u = ufos[i];
            if (u.x > hero.x - u.img.width && u.x < hero.x + hero.img.width && u.y > hero.y - u.img.height && u.y < hero.y + hero.img.height) {
                if (u.type == Enum.UFO_TYPE.LIFE) {
                    Sounds.ufoGet.play();
                    hero.life++;
                } else if (u.type == Enum.UFO_TYPE.BULLET) {
                    Sounds.ufoLev.play();
                    Bullet.prototype.type++;
                    if (Bullet.prototype.type >= 8) {
                        Bullet.prototype.type = 7;
                    }
                } else if (u.type == Enum.UFO_TYPE.BAOHU) {  //开启护盾
                    Sounds.ufoLev.play();
                    bhq.startBhq = true;
                    bhq.start = new Date().getTime();
                } else { //开启护盾
                    Sounds.ufoLev.play();
                    hero.type++;
                    if (hero.type >= 3) {
                        hero.type = 3;
                    }
                }
                ufos.splice(i, 1);
            }
        }

        //检测敌军子弹是否和英雄机碰撞
        for (var i = 0; i < eBullets.length; i++) {
            
            var eb = eBullets[i];

            //检测是否和保护罩发生碰撞
            if (bhq.startBhq) {
                if (eb.x > bhq.x - eb.img.width && eb.x < bhq.x + bhq.imgs[0].width && eb.y > bhq.y - eb.img.height && eb.y < bhq.y + bhq.imgs[0].height) {
                    eBullets.splice(i, 1);
                    continue
                }
            }

            if (eb.x > hero.x - eb.img.width && eb.x < hero.x + hero.img.width && eb.y > hero.y - eb.img.height && eb.y < hero.y + hero.img.height) {

                eb.canDelete = true;
                eBullets.splice(i, 1);
                hero.life--;
                hero.type--;
                if (hero.type <= 0) {
                    hero.type = 0;
                }
                var hitPoint = getHitPoint(eb, hero)
                hero.boom(hitPoint.x, hitPoint.y);
                Sounds.gamerOver.play();
            }
           
        }
    }
    this.pause = ()=>{
        gamer = false
    } 
    this.end = ()=>{
        gamer = false
        globalData.hero = null
        this.view.BulletsList.bullets = []
        this.view.EBsList.eBullets = []
        this.view.EnemyList.enemies = []
        this.view.UfoList.ufos = []
    }
    this.ontouchstart = function(e) {
        var e0 = e.touches[0]
        //获取鼠标在canvas中的坐标位置
        var dx = e0.clientX - canvas.offsetLeft - 1;
        var dy = e0.clientY - canvas.offsetTop - 1;
        var lastmove_x=dx;
        var lastmove_y=dy;
        var hero = globalData.hero
        var bhq = _this.view.bhq
        canvas.ontouchmove = function(e) {
            var e0 = e.touches[0]
            var mx = e0.clientX - this.offsetLeft - 1;
            var my = e0.clientY - this.offsetTop - 1;
            
            var cx=mx-lastmove_x;
            var cy=my-lastmove_y;
            lastmove_x=mx;
            
            lastmove_y=my;
            
            if( hero.x+cx<0){
                hero.x=0
            }else if( hero.x+cx+hero.width>canvas_width){
                hero.x=canvas_width-hero.width;
            }else{
                 hero.x += cx;
            }
            
            if( hero.y+cy<0){
                hero.y=0
            }else if( hero.y+cy+hero.height>canvas_height){
                hero.y=canvas_height-hero.height;
            }else{
                 hero.y += cy;
            }
            bhq.x = hero.x - ((bhq.imgs[0].width - hero.width) / 2);
            bhq.y = hero.y - ((bhq.imgs[0].height - hero.height) / 2);

        }
       
        canvas.ontouchend = function() {
            this.ontouchmove = null;
        }
            
    }
}

function pauseGame() {

    this.btn = {
        btn_continue: new ContinueBtn(),
        btn_save: new SaveBtn(),
        btn_end: new EndBtn(),
    }
    this.show = ()=>{
        scenceList.sc2.pause()
        this.btn.btn_continue.paint();
        this.btn.btn_save.paint();
        this.btn.btn_end.paint();
    }
}
//结束画面
function endGame() {
   this.btn = {
        logo: new Logo(),
        btn_gameagain: new GameAgainBtn(),
        view_end_score: new EndScoreBtn(),
        BackGroundDrawer: new BackGroundDrawer(),
    }

    this.show = ()=>{
        scenceList.sc2.end()
        this.btn.BackGroundDrawer.paint(3);
        this.btn.logo.paint();
        this.btn.view_end_score.paint();
        this.btn.btn_gameagain.paint();
    }
   
    return
    var data = {
        nickname: name,
        score: score,
        level: level
    };
    fetch(api.up_score, {
        method: 'post',
        credentials: 'include',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: http_builder_url(data)
    }).catch(error => {
        layer.msg('分数上传错误')
    });

   

}
